package com.sskj.b70.p.activity;

import android.app.AlertDialog;
import android.text.TextUtils;

import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.common.Constants;
import com.sskj.b70.m.HttpConfig;
import com.sskj.b70.m.bean.HttpData;
import com.sskj.b70.m.bean.PayWayBean;
import com.sskj.b70.m.bean.UserInfo;
import com.sskj.b70.m.http.JsonCallBack;
import com.sskj.b70.p.base.BasePresenter;
import com.sskj.b70.utils.SPUtils;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.activity.SecurityCenterActivity;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2018/4/2 0002.
 */

public class SecurityCenterPresenter extends BasePresenter<SecurityCenterActivity> {
    public void request() {
        httpService.getUserInfo()
                .execute(new JsonCallBack<HttpData<UserInfo>>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData<UserInfo>> response) {
                        HttpData<UserInfo> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.STATUS_OK) {
                            SPUtils.putBean(mView, Constants.SP_USER_INFO, httpData.getData());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, httpData.getData().getStatus());
                            mView.updataStatus(httpData.getData());
                        }
                    }
                });
    }

    public void requsetPayWayList() {
        httpService.getPayList()
                .execute(new JsonCallBack<HttpData<List<PayWayBean>>>(this, false) {
                    @Override
                    protected void _onSuccess(Response<HttpData<List<PayWayBean>>> body) {
                        mView.onPayWaySuccess(body.body().getData());
                    }

                    @Override
                    public void onError(Response<HttpData<List<PayWayBean>>> response) {
                        super.onError(response);
                        mView.onHttpFail();
                    }
                });
    }

    public void uploadImg(File file) {
        httpService.uploadImg(file)
                .execute(new JsonCallBack<HttpData<String>>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData<String>> response) {
                        HttpData<String> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.STATUS_OK) {
                            mView.updatePath(httpData.getData());
                        }
                    }
                });
    }

    public void requsetCheckSwitch(PayWayBean item) {
        // 0 关--禁用  1  开--启用
//        int status = item.getStatus() == 0 ? 1 : 0;
        boolean flag = item.isDisable();
        httpService.closeOrUp(item)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData> response) {
                        item.setDisable(!flag);
                        mView.onChangeSwitchSuccess(item);
                    }
                });
    }

    public void addPayWay(String payId,String payType,String tradePwd,String wxAccount,String wxImg,String alipayAccount,String aliImg,String bankCardNo,String bankCardOpenBank,String bankCardType){
        httpService.addPayWay(payId,payType,tradePwd,wxAccount,wxImg,alipayAccount,aliImg,bankCardNo,bankCardOpenBank,bankCardType)
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    protected void _onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == HttpConfig.STATUS_OK) {
                            requsetPayWayList();
                            mView.onChangePayWaySuccess();
                        }
                    }
                });
    }
    public void requestAddPayWay(String type, String selectName, String bankName, String bankBranch, String bankNum, String aliaccount, String imagePath, String payPwd) {

        HttpParams params = new HttpParams();
        if (!TextUtils.isEmpty(imagePath)) {
            File file = new File(imagePath);
//            params.put("wx_img", file);
//            params.put("ali_img ", file);
            params.put("wxImgUrl", file);
            params.put("aliImgUrl ", file);
        }
        params.put("act", type);
        params.put("type", selectName);
        params.put("bank", bankName);
        params.put("branch", bankBranch);
        params.put("bank_car", bankNum);
        params.put("wx", aliaccount);
        params.put("alipay", aliaccount);
        params.put("tpwd", payPwd);
        params.put("account", MyAppLication.getAccount());

        params.put("id", MyAppLication.getID());
        params.put("payType", selectName);  // 支付方式
        params.put("alipayAccount ", aliaccount);  // 支付宝账号
        params.put("tradePwd", payPwd);  // 交易密码
        params.put("bankCardType", bankName);  // 开户银行
        params.put("bankCardOpenBank", bankBranch);  // 开户支行
        params.put("bankCardNo", bankNum);  // 银行卡号
        params.put("payId", type);  // 修改支付方式的 id
        if (TextUtils.isEmpty(type)) {
            params.remove("payId");
        }

        httpService.addZhiFu(params)
                .execute(new JsonCallBack<HttpData<Object>>(this) {
                    @Override
                    protected void _onSuccess(Response<HttpData<Object>> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (response.body().getStatus() == HttpConfig.STATUS_OK) {
                            requsetPayWayList();
                            mView.onChangePayWaySuccess();
                        }
                    }
                });
    }

    public void requestRelieveBusiness(AlertDialog alertDialog) {
//        OkGo.<HttpData<Object>>post(HttpConfig.BASE_URL + HttpConfig.RELIEVE_BUSINESS)
//                .params("account", MyAppLication.getAccount())
        httpService.applyShop(MyAppLication.getID(), "5")
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    protected void _onSuccess(Response<HttpData> response) {
                        ToastUtil.showShort(response.body().getMsg());
                        if (alertDialog.isShowing()) {
                            alertDialog.dismiss();
                        }
                        mView.onRelieveBusinessSuccess();
                    }
                });
    }
}
